<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>FreeMarker</title>
    <link rel="stylesheet" href="/static/js/layui/css/layui.css">
</head>
<style>
    body{
        margin: 20px 0px 20px 20px;
    }
</style>
<body>
<div class="layui-container">
    <div style="display: none;" >
        <textarea id="baseTr">
            <tr>
                <td>
                    <input type="text" name="column" required  lay-verify="required" placeholder="字段" autocomplete="off" class="layui-input">
                </td>
                <td>
                  <select name="type" lay-verify="required">
                    <option value="VARCHAR" selected>VARCHAR</option>
                    <option value="INT">INT</option>
                    <option value="BIGINT">BIGINT</option>
                    <option value="DECIMAL">DECIMAL</option>
                    <option value="FLOAT">FLOAT</option>
                    <option value="DOUBLE">DOUBLE</option>
                    <option value="TEXT">TEXT</option>
                    <option value="DATE">DATE</option>
                    <option value="DATETIME">DATETIME</option>
                    <option value="TIMESTAMP">TIMESTAMP</option>
                  </select>
                </td>
                <td>
                    <input type="number" name="length" value="10" required  lay-verify="required" placeholder="长度" autocomplete="off" class="layui-input">
                </td>
                <td>
                    <input type="number" name="point" value="0"  required  lay-verify="required" placeholder="小数点" autocomplete="off" class="layui-input">
                </td>
                <td>
                    <input type="text" name="defaultValue"  placeholder="默认值" autocomplete="off" class="layui-input">
                </td>
                <td>
                    <select name="isNull" lay-verify="required">
                        <option value="1" selected>是</option>
                        <option value="2">否</option>
                    </select>
                </td>
                <td>
                     <select name="isPrimaryKey" lay-verify="required">
                        <option value="1" selected>是</option>
                        <option value="2">否</option>
                    </select>
                </td>
                <td>
                    <input type="text" name="columnRemarks" required  lay-verify="required" placeholder="注释" autocomplete="off" class="layui-input">
                </td>
                 <td>
                     <button type="button" class="delTr layui-btn layui-btn-danger" >删除</button>
                </td>
            </tr>
        </textarea>
    </div>
    <form class="layui-form" action="" lay-filter="create-from" >
    <div class="layui-row" >
        <hr class="layui-bg-green">
        <div class="layui-col-md6">
            <div class="layui-form-item">
                <label class="layui-form-label">表名</label>
                <div class="layui-input-block">
                    <input type="text" name="tableName" required  lay-verify="required" placeholder="请输入表名" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-col-md6">
            <div class="layui-form-item">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <input type="text" name="remarks" required  lay-verify="required" placeholder="请输入述" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row">
        <hr class="layui-bg-green">
        <div class="layui-col-md4">
            <button type="button" class="layui-btn layui-btn-normal" id="addTr">新增字段</button>
        </div>
    </div>
        <div class="layui-row">
            <hr class="layui-bg-green">
            <div class="layui-col-md12">
                <table class="layui-table" id="table">
                    <colgroup>
                        <col width="200">
                        <col width="300">
                        <col width="150">
                        <col width="150">
                        <col width="200">
                        <col width="200">
                        <col width="200">
                        <col width="250">
                        <col width="150">
                    </colgroup>
                    <thead>
                    <tr>
                        <th>字段名</th>
                        <th>类型</th>
                        <th>长度</th>
                        <th>小数点</th>
                        <th>默认值</th>
                        <th>不是null</th>
                        <th>是否主键</th>
                        <th>注释</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="table-body">
                    </tbody>
                </table>
            </div>
        </div>
    <div class="layui-row">
        <div class="layui-col-md6 layui-col-md-offset3">
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="createTable">创建表</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </div>
    </form>
</div>
<script src="/static/js/jquery-3.4.1.js"></script>
<script src="/static/js/layui/layui.js"></script>
</body>
<script>
    var trStr = "<tr><td><input></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
   $(function () {


       layui.use('layer', function(){
           var layer = layui.layer;
       });

       layui.use('form', function(){
           var form = layui.form;

           $('#addTr').on('click',function () {
               $('#table-body').append(baseTr);
               form.render();
           });

           //监听提交
           form.on('submit(createTable)', function(data){
               var formData = $("form").serializeArray();
               $.ajax({
                   //请求方式
                   type : "POST",
                   url : "/create",
                   data : formData,
                   dataType: 'json',
                   async: true,
                   success : function(result) {
                      if (result.code == 200 || result.code == '200') {
                          layer.msg("数据表创建成功",{icon:1,time:3000,shade:0.4},function() {
                              location.href = "/list";
                          })
                      }
                   },
                   error : function(e){
                       console.log(e);
                   }
               });
               return false;
           });
       });

       var baseTr = $("#baseTr").val();
       $('#table-body').append(baseTr);

       $('#table-body').on('click','.delTr',function () {
           $(this).closest('tr').remove();
       });

   })
</script>
</html>